package com.sike.netty.jinjie.bytebuf.reference;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class ReferenceCountedTest_02 {
    public static void main(String[] args) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        ByteBuf sliceByteBuf = byteBuf.slice();
        ByteBuf duplicateByteBuf = byteBuf.duplicate();
        ByteBuf copyByteBuf = byteBuf.copy();

        System.out.println("============初始值==================");
        System.out.println("byteBuf = " + byteBuf.refCnt());
        System.out.println("sliceByteBuf = " + sliceByteBuf.refCnt());
        System.out.println("duplicateByteBuf = " + duplicateByteBuf.refCnt());
        System.out.println("copyByteBuf = " + copyByteBuf.refCnt());

        System.out.println("============byteBuf： refCnt + 1==================");
        byteBuf.retain();
        System.out.println("byteBuf = " + byteBuf.refCnt());
        System.out.println("sliceByteBuf = " + sliceByteBuf.refCnt());
        System.out.println("duplicateByteBuf = " + duplicateByteBuf.refCnt());
        System.out.println("copyByteBuf = " + copyByteBuf.refCnt());

        System.out.println("============sliceByteBuf： refCnt + 1==================");
        sliceByteBuf.retain();
        System.out.println("byteBuf = " + byteBuf.refCnt());
        System.out.println("sliceByteBuf = " + sliceByteBuf.refCnt());
        System.out.println("duplicateByteBuf = " + duplicateByteBuf.refCnt());
        System.out.println("copyByteBuf = " + copyByteBuf.refCnt());

        System.out.println("============copyByteBuf： refCnt + 1==================");
        copyByteBuf.retain();
        System.out.println("byteBuf = " + byteBuf.refCnt());
        System.out.println("sliceByteBuf = " + sliceByteBuf.refCnt());
        System.out.println("duplicateByteBuf = " + duplicateByteBuf.refCnt());
        System.out.println("copyByteBuf = " + copyByteBuf.refCnt());
    }
}
